package com.termine.jsf;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.ListDataModel;

import org.primefaces.model.SelectableDataModel;

import com.termine.bean.Bean;
import com.termine.entity.Availability;
import com.termine.util.Logger;

@ManagedBean
@SessionScoped
public class AvailabilityJSF extends Bean implements Serializable {

	private static final long serialVersionUID = -4085480046224193090L;

	private AvailabilityDataModel availabilityDataModel;
	private Availability selectedAvailability;
	private Availability newAvailability;
	
	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		try {
		availabilityDataModel = new AvailabilityDataModel(getAvailabilityDAO().getAllAvailabilitiesForProvider(
				                                          getLoginJSF().getPersonalData()));
		newAvailability = new Availability();	
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
		}
	}
	
	/*
	 * Jsf Action Methods
	 */
	public String addAvailability() {
		try {
			getAvailabilityDAO().addAvailability(newAvailability, getLoginJSF().getPersonalData());
			init();
			return "SUCCESS";			
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}		
	}

	public String deleteAvailability() {
		try {
			getAvailabilityDAO().deleteAvailability(selectedAvailability);
			init();
			return "SUCCESS";
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}	
	}

	public String updateAvailability() {
		try {
			getAvailabilityDAO().updateAvailability(selectedAvailability);
			init();
			return "SUCCESS";
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}	
	}

	/*
	 * TableModel
	 */	  
	public class AvailabilityDataModel extends ListDataModel<Availability> implements SelectableDataModel<Availability> {    
	  
	    public AvailabilityDataModel() {}  
	  
	    public AvailabilityDataModel(List<Availability> data) {  
	        super(data);  
	    }  
	      
	    @Override 
	    public Availability getRowData(String rowKey) {
	        @SuppressWarnings("unchecked")
			List<Availability> availabilities = (List<Availability>) getWrappedData();  
	          
	        for (Availability availability : availabilities) {
	            if (availability.getAvailability_id().equals(Integer.parseInt(rowKey))) { 
	                return availability;  
	            }
	        }         
	        return null;  
	    }  
	  
	    @Override  
	    public Object getRowKey(Availability availability) {  
	        return availability.getAvailability_id();  
	    }  
	}  
	
	/*
	 * Getters & Setters
	 */	
	public AvailabilityDataModel getAvailabilityDataModel() {
		return availabilityDataModel;
	}

	public void setAvailabilityDataModel(AvailabilityDataModel availabilityDataModel) {
		this.availabilityDataModel = availabilityDataModel;
	}

	public Availability getSelectedAvailability() {
		return selectedAvailability;
	}

	public void setSelectedAvailability(Availability selectedAvailability) {
		this.selectedAvailability = selectedAvailability;
	}

	public Availability getNewAvailability() {
		return newAvailability;
	}

	public void setNewAvailability(Availability newAvailability) {
		this.newAvailability = newAvailability;
	}

}
